﻿Public Class LocalForm

    Private Sub CustomersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.LocalCRMDataSet)

    End Sub

    Private Sub LocalForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'LocalCRMDataSet.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.LocalCRMDataSet.Customers)

    End Sub

    Private Sub CustomersBindingSource_CurrentChanged _
                     (ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                      Handles CustomersBindingSource.CurrentChanged
        Dim c As LocalCRMDataSet.CustomersRow = _
                       CType(CType(Me.CustomersBindingSource.CurrencyManager.Current,  _
                                    DataRowView).Row,  _
                             LocalCRMDataSet.CustomersRow)
        If c.RowState = DataRowState.Detached Then
            c.CustomerId = Guid.NewGuid
        End If
    End Sub

    Private Sub btnSynchronize_Click(ByVal sender As Object, ByVal e As EventArgs) _
                                                           Handles btnSynchronize.Click
        Me.btnSynchronize.Enabled = False
        Me.btnSynchronizeViaService.Enabled = False

        Me.bgWorker.RunWorkerAsync(New CRMDataCacheSyncAgent())
    End Sub

    Private Sub btnSynchronizeViaService_Click(ByVal sender As System.Object, _
                                                ByVal e As System.EventArgs) _
                                                 Handles btnSynchronizeViaService.Click
        Me.btnSynchronize.Enabled = False
        Me.btnSynchronizeViaService.Enabled = False

        Me.bgWorker.RunWorkerAsync(New ServiceCRMDataCacheSyncAgent())
    End Sub

    Private Sub bgWorker_DoWork(ByVal sender As System.Object, _
                             ByVal e As System.ComponentModel.DoWorkEventArgs) _
                                                            Handles bgWorker.DoWork
        Dim syncAgent As Microsoft.Synchronization.SyncAgent = _
                               TryCast(e.Argument, Microsoft.Synchronization.SyncAgent)
        If syncAgent Is Nothing Then Return
        Dim clientProvider As  _
              Microsoft.Synchronization.Data.SqlServerCe.SqlCeClientSyncProvider = _
              CType(syncAgent.LocalProvider,  _
                    Microsoft.Synchronization.Data.SqlServerCe.SqlCeClientSyncProvider)
        AddHandler clientProvider.ApplyingChanges, AddressOf ApplyingChanges
        syncAgent.Synchronize()
    End Sub
    Private Sub ApplyingChanges(ByVal sender As Object, _
                    ByVal e As Microsoft.Synchronization.Data.ApplyingChangesEventArgs)
        Me.bgWorker.ReportProgress(25, "Applying Changes")
    End Sub

    Private Sub bgWorker_ProgressChanged(ByVal sender As Object, _
                           ByVal e As System.ComponentModel.ProgressChangedEventArgs) _
                                                       Handles bgWorker.ProgressChanged
        Me.lblSyncProgress.Text = e.UserState.ToString
    End Sub


    Private Sub bgWorker_RunWorkerCompleted(ByVal sender As System.Object, _
                        ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
                                                    Handles bgWorker.RunWorkerCompleted
        Me.CustomersTableAdapter.Fill(Me.LocalCRMDataSet.Customers)

        Me.btnSynchronize.Enabled = True
        Me.btnSynchronizeViaService.Enabled = True
    End Sub

End Class